<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>温度数据可视化</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .chart-container {
            position: relative;
            height: 70vh;
            width: 100%;
            margin-top: 20px;
        }
        .controls {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .loading {
            text-align: center;
            padding: 20px;
            font-size: 18px;
            color: #666;
        }
        .error {
            text-align: center;
            padding: 20px;
            font-size: 18px;
            color: #d9534f;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>温度数据监控面板</h1>
        
        <div class="controls">
            <button onclick="loadTemperatureData()">刷新数据</button>
            <button onclick="window.location.href='index.html'">返回首页</button>
        </div>
        
        <div class="chart-container">
            <canvas id="temperatureChart"></canvas>
        </div>
        
        <div id="loadingMessage" class="loading" style="display: none;">
            正在加载数据...
        </div>
        
        <div id="errorMessage" class="error" style="display: none;">
        </div>
    </div>

    <script>
        let temperatureChart = null;

        // 页面加载完成后自动获取数据
        window.onload = function() {
            loadTemperatureData();
        };

        // 加载温度数据
        function loadTemperatureData() {
            // 显示加载提示
            document.getElementById('loadingMessage').style.display = 'block';
            document.getElementById('errorMessage').style.display = 'none';
            
            // 如果已有图表实例，先销毁
            if (temperatureChart) {
                temperatureChart.destroy();
            }
            
            // 发送请求获取数据
            fetch('get_temperature_data.php')
                .then(response => response.json())
                .then(data => {
                    // 隐藏加载提示
                    document.getElementById('loadingMessage').style.display = 'none';
                    
                    if (data.status === 'success') {
                        renderChart(data.data);
                    } else {
                        showError('获取数据失败: ' + data.message);
                    }
                })
                .catch(error => {
                    // 隐藏加载提示
                    document.getElementById('loadingMessage').style.display = 'none';
                    showError('网络错误: ' + error.message);
                });
        }

        // 渲染图表
        function renderChart(chartData) {
            const ctx = document.getElementById('temperatureChart').getContext('2d');
            
            // 提取时间和温度数据
            const timestamps = chartData.map(item => item.datetime);
            const temperatures = chartData.map(item => item.temperature);
            
            // 创建图表
            temperatureChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: timestamps,
                    datasets: [{
                        label: '温度 (°C)',
                        data: temperatures,
                        borderColor: 'rgb(255, 99, 132)',
                        backgroundColor: 'rgba(255, 99, 132, 0.2)',
                        borderWidth: 2,
                        fill: false,
                        tension: 0.1
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        x: {
                            display: true,
                            title: {
                                display: true,
                                text: '时间'
                            }
                        },
                        y: {
                            display: true,
                            title: {
                                display: true,
                                text: '温度 (°C)'
                            }
                        }
                    },
                    plugins: {
                        title: {
                            display: true,
                            text: '温度变化趋势'
                        },
                        legend: {
                            display: true,
                            position: 'top'
                        }
                    }
                }
            });
        }

        // 显示错误信息
        function showError(message) {
            document.getElementById('errorMessage').textContent = message;
            document.getElementById('errorMessage').style.display = 'block';
        }
    </script>
</body>
</html>